package cn.tedu.atomic;

// 可见性
public class VolatileDemo {

    public static void main(String[] args) {

        Data d = new Data();
        d.i = 10;

        // 线程A
        new Thread(() -> {
            System.out.println("线程A启动~~~");
            while (d.i == 10) ;
            System.out.println("线程A结束！！！");
        }).start();

        // 线程B
        new Thread(() -> {
            // 为了给A充分的启动时间，所以让B线程先等待一会儿
            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("线程B启动~~~");
            d.i = 18;
            System.out.println("线程B结束！！！");
        }).start();

    }

}

class Data {

    volatile int i;

}
